<?php
/**
 * Created by PhpStorm.
 * User: tangmusen
 * Date: 2017/10/30
 * Time: 11:08
 */

namespace app\api\controller;


use app\admin\model\Members;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Cache;
use think\Controller;
use think\Db;
use think\Request;

class Base extends Controller
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        parent::__construct();
    }



    public function qi_niu_upload()
    {
        if(\request()->isPost()){
            $qiniu_config = config('qiniu');
            $accessKey    = $qiniu_config['ACCESSKEY'];
            $secretKey    = $qiniu_config['SECRETKEY'];
            $file         = request()->file('file');
            $filePath     = $file->getRealPath();
            $ext   = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
            $key   = substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
            $auth  = new Auth($accessKey, $secretKey);
            $token = $auth->uploadToken($qiniu_config['bucket']);
            $uploadMgr       = new UploadManager();
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
            if ($err !== null) {
                return ['status' => 0, 'msg' => '上传失败'];
            } else {
                return ['status' => 1, 'msg' =>config('qiniu.domain').'/'.$ret['key']];
            }
        }else{
            return ['status' => 0, 'msg' => '上传失败'];
        }
    }


}